package find.chat.test;

import android.app.Activity;
import android.app.Instrumentation;
import android.app.Instrumentation.ActivityMonitor;
import android.test.suitebuilder.annotation.SmallTest;
import find.chat.Login;
import find.chat.SignUp;
import find.chat.FriendList;
import find.chat.R;
import android.view.KeyEvent;
import android.view.Menu;
import android.widget.Button;
import android.widget.EditText;
import android.test.TouchUtils;
import android.test.ActivityInstrumentationTestCase2;

public class login extends ActivityInstrumentationTestCase2<Login> {
	private Login mActivity;
	Button login,cancelButton;
	EditText username, passward;
	private Instrumentation.ActivityMonitor mBrowserActivityMonitor;
	public login() {
		super(Login.class);
	}

	protected void setUp() throws Exception {
		// this method is called every time before any test execution
		super.setUp();

		mActivity = (Login) getActivity(); // get current activity
		login = (Button) mActivity.findViewById(find.chat.R.id.login);
		cancelButton = (Button) mActivity.findViewById(find.chat.R.id.cancel_login); 
		username = (EditText) mActivity.findViewById(find.chat.R.id.userName);
		passward = (EditText) mActivity.findViewById(find.chat.R.id.password);
		mBrowserActivityMonitor = new Instrumentation.ActivityMonitor(FriendList.class.getName(), null, false);
	    getInstrumentation().addMonitor(mBrowserActivityMonitor);
		// link the objects with the activity objects
	}
	
	@SmallTest
	// SmallTest: this test doesn't interact with any file system or network.
	public void testView() { // checks if the activity is created
		assertNotNull(getActivity());
	}

	public void testingno() {
		TouchUtils.tapView(this, username);
		sendKeys("");
		TouchUtils.tapView(this, passward);
		sendKeys("");
		assertEquals("",username.getText().toString());
		assertEquals("",passward.getText().toString());
		TouchUtils.clickView(this, login);}
	public void testingwrongpw() {
		TouchUtils.tapView(this, username);
		sendKeys("C H U N G");
		TouchUtils.tapView(this, passward);
		sendKeys("1 2 3 4 6");
		assertEquals("chung",username.getText().toString());
		assertEquals("12346",passward.getText().toString());
	TouchUtils.clickView(this, login);}
	public void testingwrongnm() {
		TouchUtils.tapView(this, username);
		sendKeys("C H U N F");
		TouchUtils.tapView(this, passward);
		sendKeys("1 2 3 4 5");
		assertEquals("chunf",username.getText().toString());
		assertEquals("12345",passward.getText().toString());
	TouchUtils.clickView(this, login);}
	public void testingzcorrect() {
		TouchUtils.tapView(this, username);
		sendKeys("C H U N G");
		TouchUtils.tapView(this, passward);
		sendKeys("1 2 3 4 5");
		TouchUtils.clickView(this, login);
		assertEquals("chung",username.getText().toString());
		assertEquals("12345",passward.getText().toString());
		Activity activity = mBrowserActivityMonitor.waitForActivityWithTimeout(5 * 1000);
	    assertNotNull("Activity was not started", activity);
	    activity.finish();
	}
	public void testingcancel() {
		TouchUtils.clickView(this, cancelButton);
	}
	public void testingsignup(){
	ActivityMonitor am = getInstrumentation().addMonitor(SignUp.class.getName(), null, false);

	// Click the menu option
	getInstrumentation().sendKeyDownUpSync(KeyEvent.KEYCODE_MENU);
	getInstrumentation().invokeMenuActionSync(mActivity, Menu.FIRST, 0);

	Activity a = getInstrumentation().waitForMonitorWithTimeout(am, 1000);
	assertEquals(true, getInstrumentation().checkMonitorHit(am, 1));
	a.finish();}
	public void testingExitApp()
	{
		// Click the menu option
		getInstrumentation().sendKeyDownUpSync(KeyEvent.KEYCODE_MENU);
		getInstrumentation().invokeMenuActionSync(mActivity, 2, 0);
	}
	
}

